<template>
  <div class="app-container">
    <div class="crud-group">
      <el-input
        v-model="paramsForm.mark"
        type="text"
        placeholder="请输入任务日期"
        clearable
      />
      <el-input
        v-model="paramsForm.dbName"
        type="text"
        placeholder="库名"
        clearable
      />
      <el-select
        v-model="paramsForm.flag"
        placeholder="标识"
        clearable
      >
        <el-option
          v-for="item of ['WG','JQ']"
          :key="item"
          :label="item"
          :value="item"
        />
      </el-select>
      <el-select
        v-model="paramsForm.taskState"
        placeholder="状态"
        clearable
      >
        <el-option
          v-for="item in stateOption"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        />
      </el-select>
      <el-date-picker
        v-model="paramsForm.timeRange"
        type="datetimerange"
        start-placeholder="开始时间"
        end-placeholder="结束时间"
        :default-time="['00:00:00','23:59:59']"
        value-format="yyyy-MM-dd HH:mm:ss"
        clearable
        @change="(val)=>{
          if(!val){
            this.paramsForm.timeRange = []
          }
        }"
      />

      <el-button
        type="success"
        icon="el-icon-search"
        @click="getData"
      >
        搜索
      </el-button>
      <el-button
        type="warning"
        icon="el-icon-refresh-left"
        @click="resetForm"
      >
        重置
      </el-button>
      <el-button
        type="primary"
        icon="el-icon-plus"
        @click="createTaskDialogVis = true"
      >
        创建任务
      </el-button>
    </div>

    <el-table
      ref="table"
      v-loading="isLoading"
      :data="data"
      border
      @selection-change="handleSelectionChange"
      @select-all="()=>{
        if(selection){
          this.$refs.table.clearSelection()
        }
      }"
    >
      <el-table-column
        type="selection"
        width="55"
      />
      <el-table-column
        label="任务日期"
        prop="mark"
        sortable
        fixed
        min-width="50"
      />
      <el-table-column
        label="标识"
        prop="flag"
        sortable
        fixed
        min-width="40"
      />
      <el-table-column
        label="库名"
        prop="dbName"
        sortable
        min-width="40"
      />
      <el-table-column
        label="表数量"
        prop="hdfsNumber"
        min-width="40"
      />

      <el-table-column
        label="任务开始时间"
        prop="cbRangeStartTime"
        sortable
        show-overflow-tooltip
      />

      <el-table-column
        label="任务结束时间"
        prop="endTime"
        sortable
        show-overflow-tooltip
      />

      <el-table-column
        label="状态"
        prop="taskState"
        sortable
        min-width="50"
      >
        <template slot-scope="scope">
          <el-tag
            :type="getStateColor(scope.row.taskState)"
          >
            {{ scope.row.taskState }}
          </el-tag>
        </template>
      </el-table-column>

      <el-table-column
        label="还原用时"
        prop="nasName"
        sortable
        show-overflow-tooltip
        min-width="50"
      />

      <el-table-column
        label="日志"
        prop="logPath"
        show-overflow-tooltip
        min-width="40"
      >
        <template slot-scope="scope">
          <a
            style="color: dodgerblue"
            @click="viewLog(scope.row.logPath)"
          >查看</a>
        </template>
      </el-table-column>

      <el-table-column
        label="冷备任务"
        prop="cbTime"
        sortable
        show-overflow-tooltip
        min-width="50"
      >
        <template slot-scope="scope">
          <a
            style="color: dodgerblue"
            @click="() => {
              cbTimeData = scope.row.cbTimeData
              cbTimeDataDialogVisible = true
            }
            "
          >{{ scope.row.cbTime }}</a>
        </template>
      </el-table-column>

      <el-table-column
        fixed="right"
        label="表详情"
        min-width="40"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="() => {
              cbTaskId = scope.row.id
              tableDetailDIalogVisible = true
            }
            "
          >
            查看
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-container">
      <el-pagination
        :current-page="paramsForm.current"
        :page-size="paramsForm.size"
        layout="prev, pager, next,sizes,total"
        :total="total"
        @size-change="(val)=>{
          paramsForm.current = 1
          paramsForm.size = val
          getData()
        }"
        @current-change="(val)=>{
          paramsForm.current = val
          getData(false)
        }"
      />
    </div>

    <TableNameList
      :dialog-visible="tbNameDialogVisible"
      :table-name-list="tbNameList"
      @close="()=>{
        tbNameDialogVisible = false
      }"
    />

    <TableNameList
      :dialog-visible="logDialogVisible"
      :table-name-list="log"
      @close="()=>{
        logDialogVisible = false
      }"
    />

    <TableNameList
      :dialog-visible="cbTimeDataDialogVisible"
      :table-name-list="cbTimeData"
      @close="()=>{
        cbTimeDataDialogVisible = false
      }"
    />

    <DetailDialog
      :dialog-visible="detailDialogVisible"
      :detail-data="detailData"
      @close="()=>{
        detailDialogVisible = false
      }"
    />

    <TableDetailDialog
      :dialog-visible="tableDetailDIalogVisible"
      :task-id="cbTaskId"
      @close="()=>{
        tableDetailDIalogVisible = false
      }"
    />

    <CreateTaskDialog
      :dialog-visible="createTaskDialogVis"
      :backup-task-id="selection ? selection.id : ''"
      :flag="selection ? selection.flag : ''"
      @close="()=>{
        createTaskDialogVis = false
      }"
    />
  </div>
</template>

<script>
import cbApi from '@/api/coldStandby/index'
import dateUtil from '@/utils/date'
import TableNameList from './components/TableNameListDialog.vue'
import DetailDialog from './components/DetailDialog.vue'
import TableDetailDialog from './components/CBRestore/TableDetailDialog.vue'
import CreateTaskDialog from './components/CBRestore/CreateTaskDialog.vue'

const form = {
  taskName: '',
  current: 1,
  size: 20,
  mark: '',
  dbName: '',
  dbTableName: '',
  flag: '',
  taskState: '',
  nasName: '',
  timeRange: [
    dateUtil.getDateStr(new Date(new Date().setHours(0, 0, 0, 0))),
    dateUtil.getDateStr(new Date(new Date().setHours(0, 0, 0, 0) + 24 * 60 * 60 * 1000 - 1))
  ]
}

export default {
  components: {TableNameList, DetailDialog, TableDetailDialog, CreateTaskDialog},
  data() {
    return {
      data: [],
      total: 0,
      isLoading: false,
      paramsForm: {...form},
      tbNameDialogVisible: false,
      tbNameList: '',
      logDialogVisible: false,
      log: '',
      detailDialogVisible: false,
      detailData: {},
      tableDetailDIalogVisible: false,
      cbTaskId: '',
      cbTimeDataDialogVisible: false,
      cbTimeData: '',
      createTaskDialogVis: false,
      selectedTaskId: '',
      selectedFlag: '',
      selectedName: '',
      selection: null,
      type: '统计',
      stateOption: [
        {
          label: '已完成',
          value: '已完成'
        },
        {
          label: '完成',
          value: '完成'
        },
        {
          label: '进行中',
          value: '进行中'
        },
        {
          label: '失败',
          value: '失败'
        },
        {
          label: '备份失败',
          value: '备份失败'
        },
        {
          label: '待执行',
          value: '待执行'
        }
      ]
    }
  },
  created() {
    this.getData()
  },

  methods: {
    getStateColor(state) {
      if (state === '已完成' || state === '完成') {
        return 'success'
      } else if (state === '失败' || state === '备份失败') {
        return 'danger'
      } else if (state === '进行中') {
        return ''
      } else {
        return 'warning'
      }
    },
    getData(resetPage = true) {
      this.isLoading = true
      if (resetPage) {
        this.paramsForm.current = 1
      }
      const param = {
        startTime: this.paramsForm.timeRange ? this.paramsForm.timeRange[0] : '',
        endTime: this.paramsForm.timeRange ? this.paramsForm.timeRange[1] : '',
        ...this.paramsForm
      }

      cbApi.cbTask(param)
        .then((res) => {
          this.data = res.records
          this.total = res.total
        })
        .finally(() => {
          this.isLoading = false
        })
    },
    reRun() {
      if (this.type === '统计') {
        this.reRunDialogVisible = true
        return
      } else {
        const form = {
          flag: this.selection.flag,
          order: this.selection.taskName.includes('【LT】') ? '' : 'QL',
          mark: this.selection.mark,
          dbName: this.selection.dbName
        }
        cbApi.reRunFullAudit(form)
        setTimeout(() => {
          this.$message.success('开始重跑')
          this.getData()
        }, 500)
      }
    },
    async pause() {
      this.type === '统计' ? await cbApi.pauseCbParamConfig() : await cbApi.pauseFullAudit()
      this.$message.success('暂停成功')
      this.getData()
    },
    continue() {
      if (this.type === '统计') {
        this.continueDialogVisible = true
        return
      }
      const form = {
        flag: this.selection.flag,
        order: this.selection.taskName.includes('【LT】') ? '' : 'QL',
        mark: this.selection.mark,
        dbName: this.selection.dbName,
        tableName: this.selection.tableName
      }
      cbApi.continueFullAudit(form)
      setTimeout(() => {
        this.$message.success('继续重跑')
        this.getData()
      }, 500)
    },
    handleCommand(command) {
      if (command.includes('FA')) {
        this.type = '稽核'
        this[command.replace('FA', '')]()
        return
      } else {
        this.type = '统计'
      }
      this[command]()
    },
    createRestoreTask() {
      this.createTaskDialogVis = true
    },
    resetForm() {
      this.paramsForm = {...form}
      this.getData()
    },
    handleSelectionChange(val) {
      if (val.length > 1) {
        this.$refs.table.clearSelection()
        this.$refs.table.toggleRowSelection(val[val.length - 1])
      }
      this.selection = val[val.length - 1]
    },
    viewLog(logPath) {
      cbApi.download({
        logPath
      }).then(res => {
        this.logDialogVisible = true
        this.log = res
      })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
